﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Happy.Query.Filters;
using Happy.Query.Filters.Configuration;

namespace Happy.Test.Query.Filters
{
    [TestClass]
    public class FilterDefinitionTest
    {
        [TestMethod]
        public void All_Arguments_Test()
        {
            var definition = this.MockFilterDefinition();

            var whereClause = definition.BuildWhereClause(new Dictionary<string, object>
                {
                    { "Name", "%段%" },
                    { "BeginAge", 20 },
                    { "EndAge", 30 },
                    { "DepartmentId", 1 }
                });

            Assert.AreEqual("(((Name LIKE @0) AND (Age BETWEEN @1 AND @2)) AND ((DepartmentId = @3) AND (Status = @4)))", whereClause.Clause);
        }

        [TestMethod]
        public void Partial_Arguments_Test()
        {
            var definition = this.MockFilterDefinition();

            var whereClause = definition.BuildWhereClause(new Dictionary<string, object>
                {
                    { "Name", "%段%" },
                    { "DepartmentId", 1 }
                });

            Assert.AreEqual("(((Name LIKE @0)) AND ((DepartmentId = @1) AND (Status = @2)))", whereClause.Clause);
        }

        [TestMethod]
        public void Not_Provide_First_Argument_Test()
        {
            var definition = this.MockFilterDefinition();

            var whereClause = definition.BuildWhereClause(new Dictionary<string, object>
                {
                    { "BeginAge", 20 },
                    { "EndAge", 30 },
                    { "DepartmentId", 1 }
                });

            Assert.AreEqual("(((Age BETWEEN @0 AND @1)) AND ((DepartmentId = @2) AND (Status = @3)))", whereClause.Clause);
        }

        public FilterDefinition MockFilterDefinition()
        {
            return FilterDefinition.Define(
                that =>
                {
                    that.DefineParameter("Name", required: false);
                    that.DefineParameter("BeginAge", required: false);
                    that.DefineParameter("EndAge", required: false);
                    that.DefineParameter("DepartmentId", required: true);
                },
                config =>
                {
                    return config
                        .BeginGroup()
                            .BeginGroup()
                                .Contains("Name", OperandType.Parameter, "Name")
                                .And()
                                .Between("Age", OperandType.Parameter, "BeginAge", OperandType.Parameter, "EndAge")
                            .EndGroup()
                            .And()
                            .BeginGroup()
                                .Equal("DepartmentId", OperandType.Parameter, "DepartmentId")
                                .And()
                                .Equal("Status", "审核")
                            .EndGroup()
                        .EndGroup()
                        .Done();
                });
        }
    }
}
